package it.uniroma2.exp;

import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Test extends AbstractExperiment {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Test test = new Test();
		test.setVectorSizeArray(new int[]{32});
		test.setUsePosArray(new boolean[] {false});
		test.setLexicalizedArray(new boolean[] {true});
		test.setLambdaArray(new double[] {1});
		test.runAll();
	}

	@Override
	protected void runExperiment() throws Exception {
		int blur = 50;
		String base = "/home/lorenzo/esperimenti/bi/CDS/unscaled";
		String dest = "/home/lorenzo/esperimenti/bi/CDS/blur_"+blur;
		File folder = new File(base);
		File destFolder = new File(dest);
		destFolder.mkdirs();
		for (File file : folder.listFiles()) {
			System.out.println("Blurring "+file);
			BufferedImage img = null;
			try {
				img = ImageIO.read(file);
				BufferedImage blurred = blur(img, blur);
				String destName = file.getName();
				destName = destName.substring(0, destName.indexOf("."))+"_blur"+blur+destName.substring(destName.indexOf("."));
				ImageIO.write(blurred, "png", new File(destFolder, destName));
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public BufferedImage blur(BufferedImage bufferedImage, int n) {

		float kElem = 1f / (float) (n * n);

		float[] kVector = new float[n * n];

		for (int i = 0; i < kVector.length; i++)
			kVector[i] = kElem;

		Kernel kernel = new Kernel(n, n, kVector);

		BufferedImageOp op = new ConvolveOp(kernel);
		return op.filter(bufferedImage, null);
	}

}
